<?php
// Copyright 2005 Won-Kyu Park <wkpark at kldp.org>
// All rights reserved. Distributable under GPL see COPYING
// a unicode module for the MoniWiki
//
// $Id: Test.php,v 1.2 2003/08/15 21:30:36 wkpark Exp $
//
// from http://www.randomchaos.com/document.php?source=php_and_unicode
// with some modifications
//

function utf8_to_unicode( $str ) {
    $unicode= array();        
    $values= array();
    $lookingFor= 1;
    
    for ($i= 0; $i < strlen( $str ); $i++ ) {
        $thisValue = ord( $str[ $i ] );
        if ( $thisValue < 128 ) $unicode[] = $thisValue;
        else {
            if (count( $values ) == 0)
                $lookingFor = ($thisValue < 224) ? 2 : 3;
            $values[] = $thisValue;
            if (count($values) == $lookingFor ) {
                $number= ($lookingFor == 3) ?
                    (($values[0]%16)*4096)+(($values[1]%64)*64)+($values[2]%64):
                	(($values[0]%32)*64)+($values[1]%64);
                $unicode[]= $number;
                $values= array();
                $lookingFor= 1;
            }
        }
    }
    return $unicode;
}

function unicode_to_entities( $unicode ) {
    $entities = '';
    foreach( $unicode as $value ) {
	$v=sprintf("%0x",$value);
	$entities .= '&#' . $v . ';';
    }
    return $entities;
}

function unicode_to_entities_preserving_ascii( $unicode ) {
    $entities = '';
    foreach( $unicode as $value ) {
        $entities .= ( $value > 127 ) ? '&#' . $value . ';' : chr( $value );
    }
    return $entities;
}

function strpos_unicode( $haystack , $needle , $offset = 0 ) {
    $position = $offset;
    $found = FALSE;
    
    while( (! $found ) && ( $position < count( $haystack ) ) ) {
        if ( $needle[0] == $haystack[$position] ) {
            for ($i = 1; $i < count( $needle ); $i++ ) {
                if ( $needle[$i] != $haystack[ $position + $i ] ) break;
            }
            
            if ( $i == count( $needle ) ) {
                $found = TRUE;
                $position--;
            }
        }
        $position++;
    }
    
    return ( $found == TRUE ) ? $position : FALSE;
}

$position = strpos_unicode( $unicode , utf8_to_unicode( '42' ) );

function unicode_to_utf8( $str ) {
    $utf8 = '';
    foreach( $str as $unicode ) {
        if ( $unicode < 128 ) {
            $utf8.= chr( $unicode );
        } elseif ( $unicode < 2048 ) {
            $utf8.= chr( 192 +  ( ( $unicode - ( $unicode % 64 ) ) / 64 ) );
            $utf8.= chr( 128 + ( $unicode % 64 ) );
        } else {
            $utf8.= chr( 224 + ( ( $unicode - ( $unicode % 4096 ) ) / 4096 ) );
            $utf8.= chr( 128 + ( ( ( $unicode % 4096 ) - ( $unicode % 64 ) ) / 64 ) );
            $utf8.= chr( 128 + ( $unicode % 64 ) );
        }
    }
    return $utf8;
}

// for Hangul
function utf8_hangul_to_jamo($str) {
    static $j2c=array(
        0x3131=>0x11a8,
        0x3132=>0x11a9,
        0x3133=>0x11aa,
        0x3134=>0x11ab,
        0x3135=>0x11ac,
        0x3136=>0x11ad,
        0x3137=>0x11ae,
        0x3139=>0x11af,
        0x313a=>0x11b0,
        0x313b=>0x11b1,
        0x313c=>0x11b2,
        0x313d=>0x11b3,
        0x313e=>0x11b4,
        0x313f=>0x11b5,
        0x3140=>0x11b6,
        0x3141=>0x11b7,
        0x3142=>0x11b8,
        0x3144=>0x11b9,
        0x3145=>0x11ba,
        0x3146=>0x11bb,
        0x3147=>0x11bc,
        0x3148=>0x11bd,
        0x314a=>0x11be,
        0x314b=>0x11bf,
        0x314c=>0x11c0,
        0x314d=>0x11c1,
        0x314e=>0x11c2,
        0x314f=>0x1161,
        0x3150=>0x1162,
        0x3151=>0x1163,
        0x3152=>0x1164,
        0x3153=>0x1165,
        0x3154=>0x1166,
        0x3155=>0x1167,
        0x3156=>0x1168,
        0x3157=>0x1169,
        0x3158=>0x116a,
        0x3159=>0x116b,
        0x315a=>0x116c,
        0x315b=>0x116d,
        0x315c=>0x116e,
        0x315d=>0x116f,
        0x315e=>0x1170,
        0x315f=>0x1171,
        0x3160=>0x1172,
        0x3161=>0x1173,
        0x3162=>0x1174,
        0x3163=>0x1175,
    );
    $jamo=array();
    $unicode=utf8_to_unicode($str);
    foreach ($unicode as $u) {
        if ($u >= 0xac00 and $u <=0xd7af) {
            $dummy=$u - 0xac00;
            $T= $dummy % 28 + 0x11a7;
            $dummy=(int)($dummy/28);
            $V= $dummy % 21 + 0x1161;
            $dummy=(int)($dummy/21);
            $L= $dummy + 0x1100;
            $jamo[]=$L;$jamo[]=$V;
            if ($T >=0x11a8) $jamo[]=$T;
        } else if ($u >=0x3130 and $u <=0x318f) {
            $jamo[]=$j2c[$u];
        } else {
            $jamo[]=$u;
        }
    }
    return $jamo;
}

function jamo_to_syllable($jamo) {
    define('hangul_base', 0xac00);
    define('choseong_base', 0x1100);
    define('jungseong_base', 0x1161);
    define('jongseong_base', 0x11a7);
    define('njungseong', 21);
    define('njongseong', 28);

    if (sizeof($jamo)<=3) {
        $choseong=$jamo[0];
        $jungseong=$jamo[1];
        $jongseong=isset($jamo[2]) ? $jamo[2]:0;
    }

    /* we use 0x11a7 like a Jongseong filler */
    if ($jongseong == 0)
    $jongseong = 0x11a7; /* Jongseong filler */

    if (!($choseong  >= 0x1100 && $choseong  <= 0x1112))
    return 0;
    if (!($jungseong >= 0x1161 && $jungseong <= 0x1175))
    return 0;
    if (!($jongseong >= 0x11a7 && $jongseong <= 0x11c2))
    return 0;

    $choseong  -= choseong_base;
    $jungseong -= jungseong_base;
    $jongseong -= jongseong_base;
    // php hack XXX
    $choseong = sprintf("%d",$choseong);
    $jungseong = sprintf("%d",$jungseong);
    $jongseong = sprintf("%d",$jongseong);

    $ch[0] = (($choseong * njungseong) + $jungseong) * njongseong + $jongseong
    + hangul_base;
    return $ch;
}

// vim:et:sts=4:
?>
